package com.shawn.integeration.batch.file;

import com.shawn.integeration.batch.file.listener.StudentReaderJobListener;
import com.shawn.integeration.batch.file.processor.StudentProcessor;
import com.shawn.integeration.batch.file.reader.StudentFileItemReaderBuilder;
import com.shawn.integeration.batch.file.writer.StudentItemWriter;
import com.shawn.integeration.business.student.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author shawn
 * @description: 文件数据读取任务
 * @date 2024/1/7
 */
@Slf4j
@Configuration
public class StudentReaderJobBatchConfig {
    // 任务构建工厂
    @Resource
    private JobBuilderFactory jobFactory;
    // 步骤构建工厂
    @Resource
    private StepBuilderFactory stepFactory;
    // 学生文件读取构造器
    @Resource
    private StudentFileItemReaderBuilder readerBuilder;
    // 学生信息处理器
    @Resource
    private StudentProcessor studentProcessor;
    // 学生信息写入器
    @Resource
    private StudentItemWriter studentWriter;
    // 任务监听器
    @Resource
    private StudentReaderJobListener jobListener;

    @Bean
    public Job studentReaderJob() {
        return jobFactory.get("studentReaderJob")
                .incrementer(new RunIdIncrementer())
                .listener(jobListener)
                .start(studentReaderJobStep())
                .build();
    }

    private Step studentReaderJobStep() {
        return stepFactory.get("studentReaderJobStep")
                .<Student, Student>chunk(10)
                .reader(readerBuilder.build())
                .processor(studentProcessor)
                .writer(studentWriter)
                .build();
    }
}
